Loop colours from variables for graphics.py [Python 3.2]

Posted by user1056548 on Stack Overflow See other posts from Stack Overflow or by user1056548
Published on 2011-11-20T16:41:50Z Indexed on 2011/11/26 17:50 UTC
Read the original article Hit count: 270

Filed under:
|

I am creating a graphics program that draws 100 x 100 squares next to each other depending on the user-specified grid size. The user also inputs 4 colours for the squares to be coloured (e.g. if they enter red,green,blue,yellow the squares will be coloured in that order, repeating the colours).

Is it possible to loop the colours from the variables the user has given?

Here is what I have so far:

def main():
    print ("Please enter four comma seperated colours e.g.: 'red,green,blue,yellow'\n\
    Allowed colours are: red, green, blue, yellow and cyan")
    col1, col2, col3, col4 = input("Enter your four colours: ").split(',')
    win = GraphWin ("Squares", 500, 500)
    colours = [col1, col2, col3, col4]
    drawSquare (win, col1, col2, col3, col4, colours)
    win.getMouse()
    win.close()


def drawSquare(win, col1, col2, col3, col4, colours):
    for i in range (4):
        for j in range (len(colours)):
            colour = colours[j]
            x = 50 + (i * 50)
            circle = Circle (Point (x,50), 20)
            circle.setFill(colour)
            circle.draw(win)

I think I should be using a list in some way, but can't work out exactly how to do it. Can anybody help?

© Stack Overflow or respective owner

Related posts about python

Related posts about python-3.x